package com.test;

/**
 * @Author: zhangquan
 * @Description:
 * @Date: 2020/7/30 14:17
 * @Version: 1.0
 */
public class SingletonLanHan {

    /**
     * 线程不安全不可用
     */
    private static SingletonLanHan singletonLanHan;

    private SingletonLanHan () {
    }

    public static SingletonLanHan getInstance() {
        if (singletonLanHan == null) {
            singletonLanHan = new SingletonLanHan();
        }
        return  singletonLanHan;
    }

    /**
     * 效率低
     */
    private static SingletonLanHan singletonLanHanTwo;

    public static synchronized  SingletonLanHan getSingletonLanHanTwo() {
        if (singletonLanHanTwo == null) {
            singletonLanHanTwo = new SingletonLanHan();
        }
        return singletonLanHanTwo;
    }

    /**
     * 线程不安全
     */
    private static SingletonLanHan instanceThree = null;

    public static SingletonLanHan getSingletonLanHanThree() {
        if (instanceThree == null) {
            synchronized (SingletonLanHan.class) {// 线程不安全
                instanceThree = new SingletonLanHan();
            }
        }
        return instanceThree;
    }

    /**
     * 推荐使用
     */
    private static SingletonLanHan singletonLanHanFour;

    public static SingletonLanHan getSingletonLanHanFour() {
        if (singletonLanHanFour == null) {
            synchronized(SingletonLanHan.class) {
                if (singletonLanHanFour == null) {
                    singletonLanHanFour = new SingletonLanHan();
                }
            }
        }
        return singletonLanHanFour;
    }

}
